<?php
class Admin_PageController extends Zend_Controller_Action{
	
	public function init(){
		Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
		$this->_model = new Default_Model_DbTable_Page();
		$auth = Zend_Auth::getInstance();
		$infoUser = $auth->getIdentity();
		$this->view->fullName = $infoUser->ADMINNAME;
		$this->view->role = $infoUser->ROLE_ID;
	}
	
	public function indexAction(){
		$pages = $this->_model->getAllPage();
		$this->view->allPage = $pages;
		if ($this->_request->isPost()){
			$upload = new Private_Upload();
			$name = $this->_getParam("Name");
			$parent = $this->_getParam("Parent");
			$page = $this->_getParam("PName");
			$block = $this->_getParam("Block");
			$con = $this->_getParam("Controller");
			$desc = $this->_getParam("Desc");
			$stt = $this->_getParam("Stt");
			if($_FILES["Img"]["name"]== "" and $this->_getParam("Link")==""){
				$img = "";
			}elseif ($this->_getParam("Link")){
				$img = $this->_getParam("Link");
			}else{
				$img = "/homeviet/public/uploaded/images/page/".$_FILES["Img"]["name"];
				$upload->upload("page");
			}
			$this->_model->addPage($parent, $page, $img, $name, $desc, $con, $block, $stt);
			$this->_redirect("admin/page");
		}
	}
	
	public function editAction(){
		$id = $this->_getParam("id");
		$this->view->page = $this->_model->getPageById($id);
		$this->view->allPage = $this->_model->getAllPage();
	if ($this->_request->isPost()){
			$upload = new Private_Upload();
			$name = $this->_getParam("Name");
			$parent = $this->_getParam("Parent");
			$page = $this->_getParam("PName");
			$block = $this->_getParam("Block");
			$con = $this->_getParam("Controller");
			$desc = $this->_getParam("Desc");
			$stt = $this->_getParam("Stt");
			if($_FILES["Img"]["name"]== "" and $this->_getParam("Link")==""){
				$img = "";
			}elseif ($this->_getParam("Link")){
				$img = $this->_getParam("Link");
			}else{
				$img = "/homeviet/public/uploaded/images/page/".$_FILES["Img"]["name"];
				$upload->upload("page");
			}
			$this->_model->updatePage($id,$parent, $page, $img, $name, $desc, $con, $block, $stt);
			$this->_redirect("admin/page");
		}
	}
	
	public function delAction(){
		$this->_model->removePage($this->_getParam("id"));
		$this->_redirect("admin/page");
	}
	
	public function activeAction(){
		$this->_model->activePage($this->_getParam("id"));
		$this->_redirect("admin/page");
	}
}